package com.gitee.srurl.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gitee.srurl.core.annotation.Login;
import com.gitee.srurl.core.domain.SusUserDomain;
import com.gitee.srurl.core.dto.UpdatePasswordDTO;
import com.gitee.srurl.core.service.ISusUserService;
import com.gitee.srurl.core.util.PageQuery;
import com.gitee.srurl.core.util.login.LoginHelper;
import com.gitee.srurl.core.util.login.OperationUser;
import com.gitee.srurl.core.util.valid.AddGroup;
import com.gitee.srurl.core.util.valid.UpdateGroup;
import com.gitee.srurl.system.util.PageUtil;
import com.gitee.srurl.system.util.response.PageResponse;
import com.gitee.srurl.system.util.response.R;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author zxw
 * @date 2023/5/3 10:29
 */
@RestController
@RequestMapping("user")
@RequiredArgsConstructor
public class UserController {

    private final ISusUserService userService;

    @Login
    @GetMapping("list")
    public R<PageResponse<SusUserDomain>> list(PageQuery pageQuery, SusUserDomain userDomain) {

        IPage<SusUserDomain> page = userService.getUserList(pageQuery, userDomain);
        return R.page(PageUtil.mybatisPageTransfer(page));
    }

    @Login
    @GetMapping("{userId}")
    public R<SusUserDomain> info(@PathVariable Long userId) {

        SusUserDomain userDomain = userService.getUserInfo(userId);
        return R.ok(userDomain);
    }

    @Login
    @PutMapping("{userId}")
    public R<Void> update(@PathVariable Long userId, @RequestBody @Validated(UpdateGroup.class) SusUserDomain userDomain) {

        userDomain.setId(userId);
        userService.updateUser(userDomain);
        return R.ok();
    }

    @Login
    @PostMapping
    public R<Void> add(@RequestBody @Validated(AddGroup.class) SusUserDomain userDomain) {

        userService.addUser(userDomain);
        return R.ok();
    }

    @Login
    @PostMapping("updatePassword")
    public R<Void> updatePassword(@RequestBody @Validated UpdatePasswordDTO updatePasswordDTO) {

        OperationUser loginUser = LoginHelper.getLoginUser();
        userService.updatePassword(loginUser.getUserId(), updatePasswordDTO);
        return R.ok("密码修改成功");
    }
}
